$(function () {
    $("#jqGrid").jqGrid({
        url: '../chapter/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '图书', name: 'bookName', index: 'book_id', width: 80},
            {label: '链接', name: 'link', index: 'link', width: 80},
            {label: '标题', name: 'title', index: 'title', width: 80},
            {label: '主体内容', name: 'body', index: 'body', width: 80},
            {label: '添加时间', name: 'createTime', index: 'create_time', width: 80,
                formatter:function (value) {
                    return transDate(value);
                }
            }
            ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.pageNum",
            total: "page.pages",
            records: "page.total"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });

    $('#body').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: 'auto', //高度
        language: "zh_cn",
        minHeight: '200px',
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../file/upload',//上传到本地服务器
        imageUploadParams: {id: "edit"},
        // imageManagerDeleteURL: '../sys/oss/delete',//删除图片(有问题)
        //imagesLoadURL: '../sys/oss/queryAll'//管理图片
    });

});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        chapter: {},
        q: {
            title: ''
        },
        books:[]

    },
    methods: {
        getBooks: function () {
            $.get("../book/queryAll/", function (r) {
                vm.books = r.page.list;
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.chapter = {};
            $('#body').editable('setHTML', '');
            vm.getBooks();
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)

        },
        saveOrUpdate: function (event) {
            var url = vm.chapter.id == null ? "../chapter/save" : "../chapter/update";
            vm.chapter.body = $('#body').editable('getHTML');
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.chapter),
                success: function (r) {
                    if (r.status === "00000") {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../chapter/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../chapter/info/" + id, function (r) {
                vm.chapter = r.chapter;
                $('#body').editable('setHTML', vm.chapter.body);
                vm.getBooks();
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
        }
    }
});